package com.fireline.beauty.cashier;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.ContextHelp;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.fireline.beauty.member.FkDialog;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.widgets.BatchItem;
import com.zgw.fireline.base.widgets.ControlUtil;

/**
 * 会员还款
 * */
public class HkDialog extends Dialog {

	protected int result;
	protected Shell shell;
	private Text text;
	private Text texthkje;
	/**
	 * @wbp.nonvisual location=47,462
	 */
	private final Dataset hyxfxxSet = new Dataset(
			"select * from hyxf where xfid=?", SqliteJdbcImpl.INSTANCE);
	private CLabel lblNewLabel_1;
	private CLabel label_2;
	private CLabel label_4;
	private CLabel lblNewLabel_2;
	private String xfid;
	private CLabel labSyje;
	/**
	 * @wbp.nonvisual location=133,472
	 */
	private final Batch batch = new Batch(SqliteJdbcImpl.INSTANCE);

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public HkDialog(Shell parent, String xfid) {
		super(parent, SWT.NONE);
		this.xfid = xfid;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public int open() {
		createContents();
		ControlUtil.centerShell(shell);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		hyxfxxSet.setKeyColumns(new String[] {});
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(420, 287);
		shell.setText("会员还款");
		GridLayout gl_shell = new GridLayout(1, false);
		shell.setLayout(gl_shell);

		Group group = new Group(shell, SWT.NONE);
		GridLayout gl_group = new GridLayout(4, false);
		gl_group.marginHeight = 20;
		gl_group.marginWidth = 20;
		gl_group.verticalSpacing = 20;
		gl_group.horizontalSpacing = 0;
		group.setLayout(gl_group);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		group.setText("消费信息");

		Label lblNewLabel = new Label(group, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("宋体", 9, SWT.NORMAL));
		lblNewLabel.setText("消费单号：");

		lblNewLabel_1 = new CLabel(group, SWT.NONE);
		lblNewLabel_1.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		lblNewLabel_1
				.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		GridData gd_lblNewLabel_1 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel_1.widthHint = 150;
		lblNewLabel_1.setLayoutData(gd_lblNewLabel_1);

		Label label_1 = new Label(group, SWT.NONE);
		GridData gd_label_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_label_1.horizontalIndent = 20;
		label_1.setLayoutData(gd_label_1);
		label_1.setText("消费金额：");

		label_2 = new CLabel(group, SWT.NONE);
		label_2.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		label_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));

		Label label = new Label(group, SWT.NONE);
		label.setText("挂帐时间：");

		lblNewLabel_2 = new CLabel(group, SWT.NONE);
		lblNewLabel_2.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		lblNewLabel_2
				.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));

		Label label_3 = new Label(group, SWT.NONE);
		GridData gd_label_3 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_label_3.horizontalIndent = 20;
		label_3.setLayoutData(gd_label_3);
		label_3.setText("欠款金额：");

		label_4 = new CLabel(group, SWT.NONE);
		label_4.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		label_4.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));

		Composite composite = new Composite(shell, SWT.NONE);
		GridLayout gl_composite = new GridLayout(4, false);
		gl_composite.marginHeight = 10;
		gl_composite.marginWidth = 20;
		gl_composite.verticalSpacing = 20;
		gl_composite.horizontalSpacing = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label_5 = new Label(composite, SWT.NONE);
		label_5.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		label_5.setText("本次还款：");

		texthkje = new Text(composite, SWT.BORDER);
		texthkje.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));
		texthkje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1);
		gd_text.widthHint = 100;
		texthkje.setLayoutData(gd_text);
		texthkje.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				doInputIng();
			}
		});
		ControlUtil.setCurrencyRule(texthkje, false, false);
		ControlUtil.setEnterToFocusMove(texthkje);
		Label label_6 = new Label(composite, SWT.NONE);
		label_6.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		GridData gd_label_6 = new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1);
		gd_label_6.horizontalIndent = 45;
		label_6.setLayoutData(gd_label_6);
		label_6.setText("剩余：");

		labSyje = new CLabel(composite, SWT.NONE);
		labSyje.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		labSyje.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));
		labSyje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));

		Label label_7 = new Label(composite, SWT.NONE);
		label_7.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		label_7.setText("备    注：");

		text = new Text(composite, SWT.BORDER);
		text.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		ControlUtil.setEnterToFocusMove(text);
		Label label_8 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_8.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		Composite composite_1 = new Composite(shell, SWT.NONE);
		composite_1.setLayout(new GridLayout(2, false));
		composite_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));

		Button btnNewButton_1 = new Button(composite_1, SWT.NONE);

		GridData gd_btnNewButton_1 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_1.heightHint = 25;
		gd_btnNewButton_1.widthHint = 80;
		btnNewButton_1.setLayoutData(gd_btnNewButton_1);
		btnNewButton_1.setText("确定（F5）");
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave();
			}
		});

		Button btnNewButton = new Button(composite_1, SWT.NONE);
		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton.widthHint = 80;
		gd_btnNewButton.heightHint = 25;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setText("取消（ESC）");
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = SWT.CANCEL;
				shell.close();
			}
		});
		createGUIAfter();
		setValues();
		setParams();
	}

	/**
	 * 保存还款信息
	 * */
	protected void doSave() {
		String xj = "0", czk = "0", xyk = "0", djq = "0";
		FkDialog fk = new FkDialog(shell, SWT.NONE, 3);
		fk.yfje = texthkje.getText();
		if (fk.open() == SWT.OK) {
			if (fk.fkfs.equals("xj")) {
				xj = fk.jfje;
			} else if (fk.fkfs.equals("czk")) {
				czk = fk.jfje;
			} else if (fk.fkfs.equals("xyk")) {
				xyk = fk.jfje;
			} else if (fk.fkfs.equals(" djq")) {
				djq = fk.jfje;
			}
			{
				BatchItem item = new BatchItem(batch);
				item.setName("保存还款记录 ");
				item.setCommand("insert into hkmx (hybh, xfid, je_before,je, xj, xyk, djq,czk, rq, bz, czy) values(?,?,?,?,?,?,?,?,?,?,?)");
				item.setParam(0, hyxfxxSet.getString("hybh"));
				item.setParam(1, hyxfxxSet.getString("xfid"));
				item.setParam(2, hyxfxxSet.getString("gz"));
				item.setParam(3, texthkje.getText());
				item.setParam(4, xj);
				item.setParam(5, xyk);
				item.setParam(6, djq);
				item.setParam(7, czk);
				item.setParam(
						8,
						SimpleDateFormat.getDateTimeInstance().format(
								new Date()));
				item.setParam(9, text.getText());
				item.setParam(10, ContextHelp.getUserName());
			}
			{
				BatchItem item = new BatchItem(batch);
				item.setName("修改消费记录");
				item.setCommand("update  hyxf set xj=xj+?,czk=czk+?,xyk=xyk+?,djq=djq+?,gz=gz-? where xfid=?");
				item.setParam(0, xj);
				item.setParam(1, czk);
				item.setParam(2, xyk);
				item.setParam(3, djq);
				item.setParam(4, texthkje.getText());
				item.setParam(5, hyxfxxSet.getString("xfid"));
			}
			try {
				batch.execute();
				result = SWT.OK;
				shell.close();
			} catch (Exception e) {
				MessageDialog.openError(shell, "异常", "保存失败:" + e.getMessage());
				e.printStackTrace();
			}

		}
	}

	/*
	 * 输入还款金额 计算剩余
	 */
	protected void doInputIng() {
		String hkje = texthkje.getText();
		String qkje = hyxfxxSet.getString("gz");
		BigDecimal syje = new BigDecimal(qkje).subtract(new BigDecimal(hkje));
		labSyje.setText(syje.toString());
	}

	protected void setValues() {
		lblNewLabel_1.setText(hyxfxxSet.getValue("xfid", String.class, ""));
		label_2.setText(hyxfxxSet.getValue("xfje", String.class, ""));
		label_4.setText(hyxfxxSet.getValue("gz", String.class, ""));
		lblNewLabel_2.setText(hyxfxxSet.getValue("rq", String.class, ""));
		labSyje.setText(hyxfxxSet.getValue("gz", String.class, ""));
	}

	protected void createGUIAfter() {
		hyxfxxSet.setParam(0, xfid);
		hyxfxxSet.update();
		hyxfxxSet.setSelected(0);
	}

	protected void setParams() {
	}

}
